package com.czb.servlet;

import com.czb.service.LesseeService;
import com.czb.service.LesseeServiceImpl;
import com.czb.util.PageBean;
import com.czb.util.ResultJson;
import com.czb.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/LesseeServlet")
public class LesseeServlet extends HttpServlet {
    LesseeService lesseeService = new LesseeServiceImpl();
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        if(action.equals("findAll")){
            findAll(req,resp);
        }else if(action.equals("add")){
            add(req,resp);
        }else if(action.equals("update")){
            update(req,resp);
        }else if(action.equals("delete")){
            delete(req,resp);
        }else if(action.equals("findById")){
            findById(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        int l_id = Integer.valueOf(req.getParameter("l_id"));
        String sql = "delete from lessee where l_id = ?";
        int i = lesseeService.delete(sql,l_id);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        String l_name = req.getParameter("l_name");
        String l_tel = req.getParameter("l_tel");
        String l_sex=req.getParameter("l_sex");
        String l_idcard=req.getParameter("l_idcard");
        String l_nativeplace=req.getParameter("l_nativeplace");
        int l_id = Integer.valueOf(req.getParameter("l_id"));
        String l_addtime=req.getParameter("l_addtime");
        Object[] obj = {l_name,l_tel,l_sex,l_idcard,l_nativeplace,l_addtime,l_id};
        String sql = "update lessee set l_name = ?,l_tel = ?,l_sex = ?,l_idcard = ?,l_nativeplace = ?,l_addtime = ?  where l_id = ?";
        int i = lesseeService.update(sql,obj);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp) {
        int l_id = Integer.valueOf(req.getParameter("l_id"));
        String sql = "select *  from lessee where l_id = ?";
        Map map = lesseeService.findById(sql,l_id);
        ResultJson success = ResultJson.success(map);
        Utils.returnJson(success,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        String l_name = req.getParameter("l_name");
        String l_tel = req.getParameter("l_tel");
        String l_sex=req.getParameter("l_sex");
        String l_idcard=req.getParameter("l_idcard");
        String l_nativeplace=req.getParameter("l_nativeplace");
        String l_addtime=req.getParameter("l_addtime");
        Object[] obj = {l_name,l_tel,l_sex,l_idcard,l_nativeplace,l_addtime};
        String sql = "insert into lessee values(null,?,?,?,?,?,?)";
        int i = lesseeService.add(sql,obj);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = req.getParameter("page") != null ? Integer.valueOf(req.getParameter("page")) : 1;
        Integer limit = req.getParameter("limit") != null ? Integer.valueOf(req.getParameter("limit")) : 10;
        String  l_name =req.getParameter("l_name")!=null?"%"+req.getParameter("l_name")+"%":"%%";
        String  l_idcard =req.getParameter("l_idcard")!=null?"%"+req.getParameter("l_idcard")+"%":"%%";
        System.out.println(l_name+l_idcard);
        String sql = "select *  from lessee where 1=1 and l_name like ? and l_idcard like ?";
        PageBean<Map> pageBean = lesseeService.findAll(page,limit,sql,l_name,l_idcard);
        System.out.println(pageBean);
        ResultJson success = ResultJson.success(pageBean);
        Utils.returnJson(success,resp);
    }
}
